from django.shortcuts import render
from django.http import HttpResponse,HttpResponseRedirect
import sys
sys.path.append(r'../')
from paper.models import QuestionType,Question
from user.models import User,PaperName
from django.urls import reverse
from learnuser.models import LearnUser,Query



#做完试卷提交后保存到数据库
def result(request,user_id,stu_user_id,paper_name):
    userid = User.objects.get(userid = user_id)
    paperid = userid.papername_set.get(paper_name_text = paper_name).id
    tiku = eval(request.POST['tiku'])
    shuliang = eval(request.POST['numb'])
    leixing = eval(request.POST['question_type_list'])
    #return render(request,'result/result.html')
    a = []
    xiede = []
    for question_id in tiku:
        question = Question.objects.get(pk = question_id)
        a.append(question)
        xiede.append(request.POST[str(question_id)])
    ss = LearnUser.objects.get(pk = stu_user_id)
    q = ss.query_set.create(query_name = ss.student.student_name,paper_name_id = paperid)
    q.topicnum_set.create(question_id = tiku,question_num = shuliang,question_type = leixing,write = xiede)
    return HttpResponse('提交试卷成功')


    



#试卷打分渲染出所有试卷名字
def roll(request,user_id):
    userid = User.objects.get(userid = user_id)
    paper_name_list = userid.papername_set.all()
    context = {'user_id':user_id,'paper_name_list':paper_name_list}
    return render(request,'result/roll.html',context)




#选择试卷名字进入到做过这份试卷的所有人。
def roll_name(request,user_id,paper_id):

    paper_obj = PaperName.objects.get(pk = paper_id)
    name_list = paper_obj.query_set.all()
    context = {'user_id':user_id,'paper_id':paper_id,'name_list':name_list}
    return render(request,'result/roll_name.html',context)






#渲染出所点学生名字做过的题和答案
def correct(request,user_id,name_id,paper_id):

    name_text = Query.objects.get(pk = name_id)
    
    question_obj = name_text.topicnum_set.all()[0]
    question_id_list = eval(question_obj.question_id)
    #所有题目的id
    question_num_list = eval(question_obj.question_num)
    #每种类型题目的数量
    question_type_list = eval(question_obj.question_type)
    #试卷的题目类型
    write_list = eval(question_obj.write)
    #学生所做的答案
    question_list = []
    for i in question_id_list:
        question_list.append(Question.objects.get(pk = i))


    context = {'user_id':user_id,'paper_id':paper_id,'name_id':name_id,'question_type_list':question_type_list,'question_list':question_list,'write_list':write_list}
    return render(request,'result/correct.html',context)





#为学生打分后分数保存到数据库
def score(request,user_id,name_id,paper_id):
    score = request.POST['score']
    name_obj = Query.objects.get(pk = name_id)
    name_obj.query_score = score
    name_obj.save()
    return HttpResponseRedirect(reverse('result:roll_name',args = (user_id,paper_id,)))
    





#学生查看做过的试卷
def zuo_paper(request,paper_id,name_id,stu_user_id):

    name_text = Query.objects.get(pk = name_id)
    question_obj = name_text.topicnum_set.all()[0]
    question_id_list = eval(question_obj.question_id)
    #所有题目的id
    question_num_list = eval(question_obj.question_num)
    #每种类型题目的数量
    question_type_list = eval(question_obj.question_type)
    #试卷的题目类型
    write_list = eval(question_obj.write)
    #学生所做的答案
    question_list = []
    for i in question_id_list:
        question_list.append(Question.objects.get(pk = i))


    context = {'paper_id':paper_id,'name_id':name_id,'question_type_list':question_type_list,'question_list':question_list,'write_list':write_list}
    return render(request,'result/zuo_paper.html',context)








